<?php
/**
 * Niushop商城系统 - 团队十年电商经验汇集巨献!
 * =========================================================
 * Copy right 2015-2025 广西软甲士网络科技有限公司, 保留所有权利。
 * ----------------------------------------------
 * 官方网址: https://www.niushop.com
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用。
 * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
 * =========================================================
 * @author : niuteam
 */

namespace app\api\controller;

use app\model\system\Address as AddressModel;

use think\facade\Db;

/**
 * 地址管理
 * @author Administrator
 *
 */
class Address extends BaseApi
{

    /**
     * 基础信息
     */
    public function info()
    {
        $id      = $this->params['id'];
        $address = new AddressModel();
        $info    = $address->getAreaInfo($id);
        return $this->response($info);
    }

    /**
     * 列表信息
     */
    public function lists()
    {
        $pid     = isset($this->params['pid']) ? $this->params['pid'] : 0;
        $address = new AddressModel();
        $list    = $address->getAreas($pid);
        return $this->response($list);
    }

    /**
     * 树状结构信息
     */
    public function tree()
    {
        $id      = $this->params['id'];
        $address = new AddressModel();
        $tree    = $address->getAreas($id);
        return $this->response($tree);
    }
	
	/**
     * 获取省份
    */
	public function getProvince()
	{
		$list = Db::name('area')->where('level', 1)->field('id,name')->order('sort','asc')->select();
		return $this->response($this->success($list));
	}
	
	/**
     * 获取城市
    */
	public function getCity()
	{
		$province_id = $this->params['province_id'];
		if (empty($province_id)){
			return $this->response($this->error('', '请传入省份ID'));
		}
		$list = Db::name('area')->where(['pid' => $province_id, 'level' => 2])->field('id,name')->select()->toArray();
        array_unshift($list, ['id' => 0, 'name' => '全部城市']);
		return $this->response($this->success($list));
	}
	
	/**
     * 获取区县
    */
	public function getDistrict()
	{
		$city_id = $this->params['city_id'];
		if (empty($city_id)){
			return $this->response($this->error('', '请传入城市ID'));
		}
		$list = Db::name('area')->where(['pid' => $city_id, 'level' => 3])->field('id,name')->select()->toArray();
        array_unshift($list, ['id' => 0, 'name' => '全部区县']);
		return $this->response($this->success($list));
	}

}